package rdb.model;

import java.util.ArrayList;
import java.util.HashMap;

public class HashObject extends BaseObject {
    private HashMap<String, byte[]> hash;

    public HashObject(BaseObject baseObject, HashMap<String, byte[]> mm) {
        super(baseObject);
        hash = mm;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("Hash: ");
        for (String field : hash.keySet()) {
            sb.append(field).append(", ").append(new String(hash.get(field))).append("\n");
        }
        return sb.toString();
    }

    @Override
    public Object getValue() {
        return hash;
    }

    @Override
    public String[] getCmdLines() {
        String[] ret = new String[2+hash.size()*2];
        int i = 0;
        ret[i++] = "hset";
        ret[i++] = getKey();
        for (String field : hash.keySet()) {
            ret[i++] = field;
            ret[i++] = new String(hash.get(field));
        }
        return ret;
    }
}
